<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="25">
        <el-button-group>
          <el-button type="primary" icon="el-icon-back" @click="backward">返回</el-button>
          <el-button type="primary" icon="el-icon-share" @click="addDialogVisible = true">添加计划</el-button>
        </el-button-group>
      </el-row>
      <el-row :gutter="25">
      </el-row>
      <!-- 产品列表 -->
      <el-table :data="planList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="设备ID" prop="plan_equ_id"></el-table-column>
        <el-table-column label="设备名称" prop="plan_equ_name"></el-table-column>
        <el-table-column label="设备编号" prop="plan_equ_code"></el-table-column>
        <el-table-column label="设备类别" prop="plan_equ_cata"></el-table-column>
        <el-table-column label="开始时间" prop="plan_start_date"></el-table-column>
        <el-table-column label="结束时间" prop="plan_end_date"></el-table-column>
      </el-table>

      <!-- 创建新产品对话框 -->
      <el-dialog title="添加设备" :visible.sync="addDialogVisible" width="50%"
                 @close="addDialogClosed">
        <!-- 内容主体区域 -->
        <el-form :model="planForm" :rules="addFormRules"
                 ref="planFormRef" label-width="70px">

          <!-- 订单ID -->
          <el-form-item label="订单ID" prop="plan_ord_id">
            <el-input v-model="planForm.plan_ord_id" disabled></el-input>
          </el-form-item>

          <!-- 设备名称 -->
          <el-form-item label="设备选择" prop="ep_pro_id">
            <el-select v-model="planForm.plan_equ_id">
              <el-option :value="equip.eq_id" v-for="equip in myEquipList">{{equip.eq_code}}</el-option>
            </el-select>
          </el-form-item>

          <!-- 计划开始时间 -->
          <el-form-item label="开始时间" prop="plan_start_date">
            <el-input v-model="planForm.plan_start_date" type="date"></el-input>
          </el-form-item>

          <!-- 计划结束时间 -->
          <el-form-item label="结束时间" prop="plan_end_date">
            <el-input v-model="planForm.plan_end_date" type="date"></el-input>
          </el-form-item>

        </el-form>
        <!-- 内容底部区域 -->
        <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addPlan">确 定</el-button>
      </span>
      </el-dialog>

      <!-- 分页 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data(){
    return{
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 5,
        fac:'',
        equ:'',
        ord:'',
      },
      fac_id:'',
      ord_id:'',
      planList:[],
      myEquipList:[],

      planForm:{
        plan_ord_id:'',
        plan_equ_id:'',
        plan_start_date:'',
        plan_end_date:'',
      },

      addFormRules:{

      },
      addDialogVisible: false,
    }
  },
  created() {
    this.getTempOrdId();
    this.getTempFacId();
    this.getPlanList();
    this.queryInfo.ord = this.ord_id;
    this.planForm.plan_ord_id = this.ord_id;
    this.getMyEquipList();
  },
  methods:{
    getTempFacId(){
      this.fac_id = window.sessionStorage.getItem("fac_id");
    },
    // 监听pageNum改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getConfigList(); // 数据发生改变重新申请数据
    },
    getTempOrdId() {
      this.ord_id = window.sessionStorage.getItem("ord_id");
    },
    // 监听添加用户
    addDialogClosed(){
      this.$refs.planFormRef.resetFields();// 重置表单项
    },
    async getPlanList(){
      this.queryInfo.ord = this.ord_id;
      this.queryInfo.fac = 0;
      this.queryInfo.equ = 0;
      const { data: res } = await this.$http.get("myOrder/planList", {
        params: this.queryInfo
      });
      this.planList = res.data; // 将返回数据赋值
      this.total = res.numbers; // 总个数
    },
    //返回上一页
    backward(){
      this.$router.push("/myOrder");
    },
    // 监听pageSize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getConfigList(); // 数据发生改变重新申请数据
    },
    async getMyEquipList(){
      const { data: res } = await this.$http.post("myEquip/allMyEquipList?id=" + this.fac_id);
      this.myEquipList = res.data;
    },
    addPlan(){
      this.$refs.planFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        //绑定当前配置信息
        this.planForm.plan_ord_id = this.ord_id;
        const {data:res} = await this.$http.post("myOrder/addPlan",this.planForm);
        if (res !== "success") {
          return this.$message.error("操作失败！！！");
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.addDialogVisible = false;
        this.getPlanList();
      })
    }
  }
}
</script>

<style scoped>

</style>